package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;



    @Override
    // 分页查询房间列表
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage, queryVo);
    }

    @Override
    // 根据公寓id分页查询房间列表
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage, id);
    }

    @Override
    // 跟根据房间id获取房间的详细信息
    public RoomDetailVo getApartmentDetail(Long id) {
        String key = "app:room:" + id;
        RoomDetailVo roomDetailVo = (RoomDetailVo) redisTemplate.opsForValue().get(key);
        if (roomDetailVo != null) {
            return roomDetailVo;
        }else{


            // 1.根据房间id查询房间的详细信息
            RoomInfo roomInfo = roomInfoMapper.selectById(id);
            //根据房间id查询公寓列表
            ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
            //获取这个公寓的标签列表
            List<LabelInfo> apartmentlabelInfoList = labelInfoMapper.listLabelInfoByApartmentId(apartmentInfo.getId());

            //获取这个公寓的图片列表
            List<GraphVo> apartmentGraphVoList = graphInfoMapper.list(apartmentInfo.getId());

            //计算这个公寓所有房间中最低租金
            List<RoomInfo> roomInfoList = roomInfoMapper.listByApartmentId(apartmentInfo.getId());
            BigDecimal minRent = roomInfoList.stream().map(RoomInfo::getRent).min(BigDecimal::compareTo).get();
            ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
            BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
            apartmentItemVo.setLabelInfoList(apartmentlabelInfoList);
            apartmentItemVo.setGraphVoList(apartmentGraphVoList);
            apartmentItemVo.setMinRent(minRent);



            // 2.根据房间id查询房间的图片列表
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId, id)
                    .eq(GraphInfo::getItemType, ItemType.ROOM);
            List<GraphInfo> list = graphInfoService.list(queryWrapper);
            //封装到GraphVo中
            List<GraphVo> graphVoList = new ArrayList<>();
            for (GraphInfo graphInfo : list) {
                GraphVo graphVo = new GraphVo();
                graphVo.setName(graphInfo.getName());
                graphVo.setUrl(graphInfo.getUrl());
                graphVoList.add(graphVo);
            }


            // 3.根据房间id获取房间属性列表
            List<AttrValueVo> attrValueVoList = roomAttrValueMapper.listByRoomId(id);

            // 4.根据房间id获取房间配套列表
            List<FacilityInfo> facilityInfoList = roomFacilityMapper.listFacilityInfoByRoomId(id);

            // 5.根据房间id获取房间标签列表
            List<LabelInfo> labelInfoList = roomLabelMapper.listByRoomId(id);

            // 6.根据房间id获取房间的支付方式列表
            List<PaymentType> paymentTypeList = roomPaymentTypeMapper.listPaymentTypeByRoomId(id);

            // 7.根据公寓id获取房间的杂费列表
            List<FeeValueVo> feeValueVoList = feeValueMapper.listFeeValueByRoomId(apartmentInfo.getId());

            // 8.根据房间id获取房间的可选租期列表
            List<LeaseTerm> leaseTermList = leaseTermMapper.listByRoomId(id);

            // 9.封装所有结果
            roomDetailVo = new RoomDetailVo();
            BeanUtils.copyProperties(roomInfo, roomDetailVo);
            roomDetailVo.setApartmentItemVo(apartmentItemVo);
            roomDetailVo.setGraphVoList(graphVoList);
            roomDetailVo.setFacilityInfoList(facilityInfoList);
            roomDetailVo.setAttrValueVoList(attrValueVoList);
            roomDetailVo.setLabelInfoList(labelInfoList);
            roomDetailVo.setPaymentTypeList(paymentTypeList);
            roomDetailVo.setLeaseTermList(leaseTermList);
            roomDetailVo.setFeeValueVoList(feeValueVoList);


            redisTemplate.opsForValue().set(key, roomDetailVo);

        }
        return roomDetailVo;
    }
}




